//
//  ModuleListController.m
//  微看
//
//  Created by 吴明磊 on 15/8/17.
//  Copyright © 2015年 wuminglei. All rights reserved.
//

#import "ModuleListController.h"
#import "HeadLineController.h"
#import "HappyController.h"
#import "MenController.h"
#import "HandPickController.h"
#import "ChatBarController.h"
#import "BaseCell.h"

@interface ModuleListController ()
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end


@interface ModuleListController ()

@property(nonatomic,strong)HeadLineController *headLineVc;
@property(nonatomic,strong)HappyController *happyVc;
@property(nonatomic,strong)MenController *menVc;
@property(nonatomic,strong)HandPickController *handPickVc;
@property(nonatomic,strong)ChatBarController *chatBarVc;

@end
@implementation ModuleListController

static NSString * const moduleListIdentifier = @"moduleListIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self registerCellStyle];
    
    [self setCollectionViewStyle];
    
    [self notificationCenter];
    
    
    
}
- (instancetype)init {

    return [super initWithCollectionViewLayout:self.flowLayout];
}

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
    
     return [self init];
}
/// 通知中心
- (void)notificationCenter {

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"NavBarModuleListSwitchNotification" object:nil];
    
}


//MARK: 通知
- (void)receiveNotification:(NSNotification *)notification {
    
    NSInteger index = [[notification.userInfo objectForKey:@"index"] longValue];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}


/// 设置collectionView的样式
- (void)setCollectionViewStyle {

    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
}

/// 注册cell
- (void)registerCellStyle {

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:moduleListIdentifier];
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return 5;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:moduleListIdentifier forIndexPath:indexPath];
    
    [cell.contentView addSubview:[self addContentView:indexPath.item]];
    
    
    return cell;
}

- (UIView *)addContentView:(NSInteger)index {

    switch (index) {
        case 0:
            return self.headLineVc.view;
            break;
        case 1:
            return self.happyVc.view;
            break;
        case 2:
            return self.menVc.view;
            break;
        case 3:
            return self.handPickVc.view;
            break;
    }
    
    return self.chatBarVc.view;
    
}

#pragma mark <UICollectionViewDelegate>

- (void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView {

    BaseCell *cell = [[self.collectionView visibleCells] lastObject];
    
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"contentScrollNotification" object:nil userInfo:@{@"index" : @(indexPath.item)}];
}

/// 设置flowLayout布局流
- (UICollectionViewFlowLayout *)flowLayout {

    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.minimumLineSpacing = 0;
        
    }
    return _flowLayout;
}

//MARK: 懒加载
- (HeadLineController *)headLineVc {

    if (!_headLineVc) {
        _headLineVc = [[HeadLineController alloc]init];
    }
    return _headLineVc;
}

- (HappyController *)happyVc {

    if (!_happyVc) {
        _happyVc = [[HappyController alloc]init];;
    }
    return _happyVc;
}
- (MenController *)menVc {
    
    if (!_menVc) {
        _menVc = [[MenController alloc]init];
    }
    return _menVc;
}
- (HandPickController *)handPickVc {

    if (!_handPickVc) {
        _handPickVc = [[HandPickController alloc]init];
    }
    return _handPickVc;
}
- (ChatBarController *)chatBarVc {

    if (!_chatBarVc) {
        _chatBarVc = [[ChatBarController alloc]init];
    }
    return _chatBarVc;
}
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
